package com.mice.controller{
	import com.iyimo.control.Alert;
	import com.iyimo.core.YEngine;
	import com.iyimo.core.mvc.Controller;
	import com.iyimo.event.Notification;
	import com.iyimo.manager.SpriteManager;
	import com.iyimo.scene.SceneSwitcher;
	import com.mice.constant.NotifyConst;
	import com.mice.event.GameEvent;
	import com.mice.game.GameEngine;
	import com.mice.game.core.Fighter;
	import com.mice.game.trigger.SpawnTrigger;
	import com.mice.meta.FighterMeta;
	import com.mice.model.AppVO;
	import com.mice.model.fight.LevelInfoVO;
	import com.mice.view.game.GameScene;

	public class GameController extends Controller{
		public static const NAME:String = "GameController";

		/**
		 * 游戏
		 */		
		private var _scene:GameScene;

		public function GameController(){
			super();
		}

		override public function onRegister():void{
			_scene = new GameScene(SpriteManager.getSprite("game"));
			_scene.addEventListener(GameEvent.SPAWN_FIGHTER, onSpawn);

			listenNotification(NotifyConst.NOTIFY_PLAY_GAME, playGame);
		}

		/**
		 * 处理游戏开始
		 */		
		private function playGame(e:Notification):void{
			SceneSwitcher.action(_scene, null);
			var info:LevelInfoVO = LevelInfoVO.createInfo(AppVO.hostUser.level, AppVO.player.team);
			_scene.setFightInfo(info);
			
			GameEngine.instance.start(AppVO.hostUser.level);
			/*将boss添加到场景中*/
			YEngine.instance.layerManager.gameLayer.addChild(GameEngine.instance.playerBoss.style);
			YEngine.instance.layerManager.gameLayer.addChild(GameEngine.instance.aiBoss.style);
			
			AppVO.player.addEventListener(GameEvent.UPDATE_ENERGY, onUpdateEnergy);
			AppVO.player.addEventListener(GameEvent.BATTLE_SUCC, onBattleSucc);
			AppVO.player.addEventListener(GameEvent.BATTLE_LOSE, onBattleLose);
			onUpdateEnergy();
		}
		
		/**
		 * 响应view产兵点击事件
		 */		
		private function onSpawn(e:GameEvent):void{
			var meta:FighterMeta = e.data as FighterMeta;
			var f:Fighter = SpawnTrigger.instance.spawn(meta.id, meta.level, AppVO.player.team);
			YEngine.instance.layerManager.gameLayer.addChild(f.style);
		}

		/**
		 * player精力值更改
		 */		
		private function onUpdateEnergy(e:GameEvent = null):void{
			_scene.updateBtnStatus(AppVO.player.energy);
		}

		/**
		 * 处理战斗失败
		 */		
		private function onBattleLose(e:GameEvent):void{
			finishGame();
		}

		/**
		 * 处理战斗胜利
		 */		
		private function onBattleSucc(e:GameEvent):void{
			finishGame();
			Alert.show("LEVEL COMPLETE", "Continue", true, gotoNextLevel);
		}

		/**
		 * 游戏结束/成功/失败/中断
		 */		
		private function finishGame():void{
			YEngine.instance.layerManager.gameLayer.removeChildren();
			AppVO.player.removeEventListener(GameEvent.UPDATE_ENERGY, onUpdateEnergy);
		}
		
		/**
		 * 继续玩下一关
		 */		
		private function gotoNextLevel():void{
			AppVO.hostUser.level += 1;
			
			SceneSwitcher.action(_scene, null);
			var info:LevelInfoVO = LevelInfoVO.createInfo(AppVO.hostUser.level, AppVO.player.team);
			_scene.setFightInfo(info);
			GameEngine.instance.playLevel(AppVO.hostUser.level);
		}
	}
}

